<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-select v-model="dataForm.templateId" filterable clearable placeholder="模板名称">
            <el-option v-for="item in templateList" :key="item.templateId" :label="item.title" :value="item.templateId">
            </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
                <el-select v-model="dataForm.tagid" filterable clearable placeholder="用户标签">
                    <el-option v-for="item in wxUserTags" :key="item.id" :label="item.name" :value="item.id"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item>
            <el-select v-model="dataForm.communityId" filterable placeholder="小区" clearable :clear="clear()" @change="changeAddress">
              <el-option v-for="item in communityList" :key="item.id" :label="item.name" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-select v-if="dataForm.communityId != '' && dataForm.communityId != undefined"
              v-model="dataForm.buildingNoId" filterable placeholder="楼号" clearable :clear="clear()" @change="changeAddress">
              <el-option v-for="item in buildingNoList" :key="item.id" :label="item.name" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-select v-if="dataForm.buildingNoId != '' && dataForm.buildingNoId != undefined"
              v-model="dataForm.unitId" filterable placeholder="单元" clearable >
              <el-option v-for="item in unitIdList" :key="item.id" :label="item.name" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button v-if="isAuth('wx:wxtemplatelog:save')" type="success" @click="addOrUpdateHandle()" >发送消息</el-button>
        <el-button v-if="isAuth('wx:wxtemplatelog:save')" type="success" @click="addOrUpdateHandle(dataListSelections[0].id)" :disabled="dataListSelections.length!=1">复制发送</el-button>
        <!-- <el-button v-if="isAuth('wx:wxtemplatelog:delete')" type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button> -->
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>
      <el-table-column
        prop="id"
        header-align="center"
        align="center"
        label="id">
      </el-table-column>
      <!-- <el-table-column
        prop="appid"
        header-align="center"
        align="center"
        label="appid">
      </el-table-column> -->
      <!-- <el-table-column
        prop="tagid"
        header-align="center"
        align="center"
        label="标签id">
      </el-table-column> -->
      <el-table-column
        prop="tagName"
        header-align="center"
        align="center"
        label="标签名">
      </el-table-column>
      <!-- <el-table-column
        prop="addressId"
        header-align="center"
        align="center"
        label="地址id">
      </el-table-column> -->
      <el-table-column
        prop="addressName"
        header-align="center"
        align="center"
        label="地址名">
      </el-table-column>
      <!-- <el-table-column
        prop="templateId"
        header-align="center"
        align="center"
        label="公众号模板ID">
      </el-table-column> -->
      <el-table-column
        prop="templateName"
        header-align="center"
        align="center"
        label="模版名称">
      </el-table-column>
      <!-- <el-table-column
        prop="data"
        header-align="center"
        align="center"
        label="消息内容">
      </el-table-column> -->
      <el-table-column
        prop="sendData"
        header-align="center"
        align="center"
        show-overflow-tooltip
        label="发送的内容">
      </el-table-column>
      <!-- <el-table-column
        prop="dataName"
        header-align="center"
        align="center"
        label="消息内容名">
      </el-table-column>
      <el-table-column
        prop="url"
        header-align="center"
        align="center"
        label="链接">
      </el-table-column> -->
      <!-- <el-table-column
        prop="miniprogram"
        header-align="center"
        align="center"
        label="小程序信息">
      </el-table-column> -->
      <el-table-column
        prop="sendDate"
        header-align="center"
        align="center"
        label="发送时间">
      </el-table-column>
      <el-table-column
        prop="sender"
        header-align="center"
        align="center"
        label="发送人">
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" @click="check(scope.row.id)">查看</el-button>
          <!-- <el-button type="text" size="small" @click="deleteHandle(scope.row.id)">删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
    <wx-user-tags-manager ref="wxUserTagsEditor" :visible="showWxUserTagsEditor" @close="showWxUserTagsEditor=false"></wx-user-tags-manager>
    <wx-user-tagging ref="wxUserTagging" :wxUsers="dataListSelections"></wx-user-tagging>
  </div>
</template>

<script>

import WxUserTagsManager from '@/components/wx-user-tags-manager'
import WxUserTagging from './wx-user-tagging'
  import AddOrUpdate from './wx-template-log-add-or-update'
  import { treeDataTranslate, treeDataSelect } from '@/utils'
  import { mapState } from 'vuex'
  export default {
    data () {
      return {
        dataForm: {
          key: '',
          tagid: '',
          communityId: '',
          buildingNoId: '',
          unitId: '',
        },
        templateList: [],
        communityList:[],
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        showWxUserTagsEditor:false,
        dataListSelections: [],
        addOrUpdateVisible: false
      }
    },
    components: {
      AddOrUpdate,WxUserTagsManager,WxUserTagging
    },
    activated () {
      this.getDataList()
      this.selectTemplate()
      this.selectAddress()
    },
    computed: mapState({
        wxUserTags:state=>state.wxUserTags.tags
    }),
    methods: {
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/wx/wxtemplatelog/list'),
          method: 'get',
          params: this.$http.adornParams({
            'page': this.pageIndex,
            'limit': this.pageSize,
            'templateId': this.dataForm.templateId,
            'tagid': this.dataForm.tagid,
            'communityId':this.dataForm.communityId,
            'buildingNoId':this.dataForm.buildingNoId,
            'unitId':this.dataForm.unitId,
          })
        }).then(({data}) => {
          if (data && data.code === 200) {
            if(this.pageIndex!=1&&data.page.list.length==0){
                        this.pageIndex-=1
                        this.getDataList()
                    }else{
                        this.dataList = data.page.list
                        this.totalPage = data.page.totalCount
                    }
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      clear(){
            if (this.dataForm.communityId == '') {
            this.$set(this.dataForm, 'buildingNoId', '')
            this.$set(this.dataForm, 'unitId', '')
          } else if (this.dataForm.buildingNoId == '') {
            this.$set(this.dataForm, 'unitId', '')
          }
        },
        selectAddress() {
      this.$http({
        url: this.$http.adornUrl('/sys/address/list'),
        method: 'get',
        params: this.$http.adornParams()
      }).then(({ data }) => {
        if (data && data.code === 200) {
          this.communityList = treeDataTranslate(data.page)
          return this.communityList
        }
      })
    },
      selectTemplate() {
      this.$http({
        url: this.$http.adornUrl('/manage/msgTemplate/all'),
        method: 'get',
        params: this.$http.adornParams({
        })
      }).then(({ data }) => {
        if (data && data.code === 200) {
          this.templateList = data.page
        }
      })
    },
    changeAddress(data) {
            if (data != "") {
                var address = treeDataSelect(this.communityList, data)
                if (address.type == 0) {
                this.buildingNoList = address.children
                this.$set(this.dataForm, 'buildingNoId', '')
                this.$set(this.dataForm, 'unitId', '')
                } else if (address.type == 1) {
                this.unitIdList = address.children
                this.$set(this.dataForm, 'unitId', '')
                }
            }
    },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      check(id){
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.check(id)
        })
      },
      // 删除
      deleteHandle (id) {
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.id
        })
        this.$confirm(`确定删除?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/wx/wxtemplatelog/delete'),
            method: 'post',
            data: this.$http.adornData(ids, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      }
    }
  }
</script>
